package com.zy.nft_system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zy.nft_system.domain.Account;
import com.zy.nft_system.domain.Result;
import com.zy.nft_system.domain.dto.AccountDto;
import com.zy.nft_system.service.AccountService;
import com.zy.nft_system.mapper.AccountMapper;
import com.zy.nft_system.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
* @author 这是那头猪？
* @description 针对表【t_account(用户账号信息表)】的数据库操作Service实现
* @createDate 2022-09-28 14:40:41
*/
@Service
@Slf4j
@Transactional
public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account>
    implements AccountService{

    @Resource
    AccountMapper accountMapper;


    @Override
    public Result userAccountList() {
        //System.out.println(accountMapper.userAccountList());
        log.info("账户查到成功");
        List<AccountDto> accountDtos = accountMapper.userAccountList(null);
        accountDtos.stream().forEach(dto -> {
            //查询用户藏品数量
            Map<String, Object> map = accountMapper.cangPingNumber(dto.getId());
            dto.setRepository(String.valueOf(map.get("number")));
        });


        return Result.success("查询成功", accountDtos);
    }

    @Override
    public Result userAccountListList(String name) {
        log.info("账户模糊查到成功");
        List<AccountDto> accountDtos = accountMapper.userAccountList(name);
        accountDtos.stream().forEach(dto -> {
            //查询用户藏品数量
            Map<String, Object> map = accountMapper.cangPingNumber(dto.getId());
            dto.setRepository(String.valueOf(map.get("number")));
        });
        return Result.success("查询成功", accountDtos);
    }

    @Override
    public Result updataAccount(int uid, int overage) {
        int i = accountMapper.updataAccount(uid, overage, DateUtils.dataFormat(new Date()));
        if (i != 1) {
            log.info("未知原因修改错误！");
            return Result.error("未知原因账户金额修改错误！");
        }
        log.info("金额修改成功");
        return Result.success("金额修改成功",null);
    }
}




